/*******************************************************************************
 *
 * common.js 拆分出和组件相关部分 common-component.js
 *
 ******************************************************************************/
/**
 * 填充SELECTOR
 *
 * @param {Object}
 *            data
 * @param {Object}
 *            tagName selector name
 * @param {Object}
 *            isDef 是否需要默认值
 */
function fullSelect(data, tagName, isDef, checked, defName, hasRecord) {
    var selector = eval("$('#" + tagName + "')");
    var selected = "";
    selector.empty();
    if (isDef) {
        if (defName != "undefined" && defName != null) {
            selector.append("<option value=''>" + defName + "</option>");
        } else if (hasRecord == "undefined" || hasRecord == null) {
            selector.append("<option value=''>--\u8bf7\u9009\u62e9--</option>");
        }

    }
    if (data != null && data.length > 0) {
        for (var i = 0; i < data.length; i++) {
            if (checked != null && checked == data[i].dictCode)
                selected = "selected";
            else
                selected = "";
            selector.append("<option value=" + data[i].dictCode + "  "
                + selected + ">" + data[i].dictName + "</option>");
        }
    } else if (hasRecord == "undefined" || hasRecord == null) {
        selector.append("<option value=''>--\u65e0\u8bb0\u5f55--</option>");
    }
}
function checkStrT(obj) {
    var checkString = "`~!@#$%^&*()+=[]{}\\|;':\",/<>?";
    for (var j = 0; j < checkString.length; j++) {
        if (obj.indexOf(checkString.substring(j, j + 1)) != -1) {
            alert("上传文件名中不能有非常字符:\"" + checkString.substring(j, j + 1)
                + "\",请重新命名!");
            return true;
        }
    }
}

function mover(src, clrOver) {
    src.bgColor = clrOver;
    for (var i = 0; i < src.childNodes.length; i++) {

        if (src.childNodes[i].childNodes[0] != undefined) {
            if (src.childNodes[i].childNodes[0].type == "text") {
                src.childNodes[i].childNodes[0].className = 'blue';
                break;
            }
        }

    }
}
// js特效
function mout(src, clrout) {
    src.bgColor = clrout;
    for (var i = 0; i < src.childNodes.length; i++) {
        if (src.childNodes[i].childNodes[0] != undefined) {
            if (src.childNodes[i].childNodes[0].type == "text") {
                src.childNodes[i].childNodes[0].className = 'white';
                break;
            }
        }

    }
}

function hasAuthoritys(rules, authority) {
    for (var i = 0; i < rules.length; i++) {
        if (rules[i] == authority)
            return true;
    }
    return false;
}

function fillSelectTag(tagName, dicArray, flag, selectedVal) {
    eval("$('#" + tagName + "option')").remove();
    if (flag) {
        eval("$('#" + tagName + "')").append(
            "<option value=''>\u8bf7\u9009\u62e9</option>");
    }
    for (var j = 0; j < dicArray.length; j++) {
        var oText = dicArray[j][1];
        var optionName = oText.charAt(0) == "#" ? oText.substring(1) : oText;
        if (oText.charAt(0) == "#") {
            selectedVar = dicArray[j][0];
        }
        eval("$('#" + tagName + "')").append(
            "<option value='" + dicArray[j][0] + "'>" + optionName
                + "</option>");
    }
    if (selectedVal != null || selectedVal != "") {
        eval("$('#" + tagName + "')").val(selectedVal);
    }
}

// 根据js数组填充下拉菜单
function fillSelectOption4Array(sel, arry) {
    // deleteAllOptions(sel);
    for (var i = 0; i < arry.length; i++) {
        addOption(sel, arry[i][1], arry[i][0]);
    }
}

// 新增下拉菜单
function addOption(sel, text, value) {
    sel.options.add(new Option(text, value));
}

// 根据js数组填充下拉菜单 mh.hu
function fillSelectOption(arry, sel, selectValue) {
    // deleteAllOptions(sel);
    for (var i = 0; i < arry.length; i++) {
        addOption(sel, arry[i][1], arry[i][0]);
    }
    sel.value = selectValue;
}

// 新增下拉菜单 mh.hu
function addOption(sel, text, value) {
    sel.options.add(new Option(text, value));
}

// 移除选中类别菜单 mh.hu
function jsRemoveSelectedItemFromSelect(objSelect) {
    var length = objSelect.options.length - 1;
    for (var i = length; i >= 0; i--) {
        if (objSelect[i].selected == true) {
            objSelect.options[i] = null;
        }
    }
}

// 清空菜单列表 mh.hu
function deleteAllOptions(sel) {
    sel.options.length = 0;
}

// mh.hu
function deleteOption(sel, indx) {
    sel.options.remove(indx);
}


// RegExt Test
function regExpTest(source, re) {
    var result = false;

    if (source == null || source == "")
        return false;

    if (source == re.exec(source))
        result = true;
    return result;
}

/**
 * 上传图片的公共JS ADD xh.wang
 *
 * @param uploadType
 *            上传类型
 * @param baseContextPath
 *            上下文路径
 * @param fileName
 *            文件名称前缀
 * @param modelType
 *            上传到那个文件夹下
 */
function imgUplaod(uploadType, baseContextPath, fileName, modelType) {
    fName = fileName + getdate();
    window
        .open(
            baseContextPath
                + "zuche/uc/common/upload/singleImageUpload.jsp?uploadType="
                + uploadType + "&modelType=" + modelType
                + "&fileName=" + fName,
            "",
            "height=600, width=800, top=20, left=20,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
}

/**
 * 查看上传图片 ADD xh.wang
 *
 * @param baseContextPath上下文路径
 * @param picInputId
 */
function openimgT(baseContextPath, picInputId) {
    var picPath = $("#" + picInputId).val();
    if (picPath == null || picPath == '') {
        alert("尚未上传图片,请上传图片后再查看");
        return;
    }
    window
        .open(
            baseContextPath
                + "zuche/uc/common/upload/openImg.jsp?img="
                + picPath,
            "",
            "status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,top=30,left=100, height=600, width=800");
}

function submitWithParam(formId, tableId) {
    var parameterString = $.jmesa.createParameterStringForLimit(tableId);
    var path = $("#" + formId).attr("action") + "&date=" + new Date().getTime();
    if (parameterString) {
        path += "&" + parameterString;
    }
    $("#" + formId).attr("action", path);
    $("#" + formId).submit();
}

/*******************************************************************************
 *
 * 关于关闭父窗口后造成的JS异常错误，提供两个函数做校验用。避免异常的发生。
 *
 ******************************************************************************/

/**
 * 判断父窗口是否已经关闭
 */
function isOpenerClosed(opener) {
    if (opener == null) {
        return true;
    } else {
        return opener.closed;
    }
}

/**
 * 判断函数是否存在
 *
 * @param functionName
 * @returns
 */
function isFunctionExist(functionName) {
    try {
        if (typeof (eval(functionName)) == "function") {
            return true;
        }
    } catch (e) {
        return false;
    }
}
/**
 * 全选js方法
 * @param nameId
 * @param checkValue
 */
function checkAllByName(nameId, checkValue) {
    var chkBoxObject = document.getElementsByName(nameId);
    if (chkBoxObject != undefined) {
        for (var i = 0; i < chkBoxObject.length; i++) {
            chkBoxObject[i].checked = checkValue;
        }
    }
}

/**
 * 计算字符串汉字长度
 * @param str
 * @returns {Number}
 */
function getChineseCount(str)	{ 
	var chineseCount = 0; 
	if (str != null) { 
		for (var i = 0; i < str.length; i++){ 
			var c = str.charAt(i); 
			if (/^[\u0000-\u00ff]$/.test(c)){ 
				chineseCount += 0.5; 
			}else { 
				chineseCount += 1; 
			} 
		} 
	} 
	return chineseCount; 
}